package com.ocm.tourism.web;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ocm.tourism.core.domain.MapInfo;
import com.ocm.tourism.core.service.MapInfoService;

@RequestMapping("/mapinfo")
@Controller
public class MapInfoController {
    @Autowired
    MapInfoService mapInfoService;
    
    @RequestMapping(value = "/map/light")
    public String viewMap(Model uiModel) {
        MapInfo mapInfo = new MapInfo();
        populateEditForm(uiModel, mapInfo);
        return "embed/google_maps_test";
    }

    @RequestMapping(method = RequestMethod.POST, produces = "text/html")
    public String create(@Valid MapInfo mapInfo, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
        if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, mapInfo);
            return "news/update";
        }
        uiModel.asMap().clear();
        mapInfoService.saveMapInfo(mapInfo);
        return "redirect:/mapinfo/map/light";
    }

    private void populateEditForm(Model uiModel, MapInfo mapInfo) {
        uiModel.addAttribute("mapInfo", mapInfo);
    }

}
